import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JComponent;


public class Player extends JComponent{
	
	private String name;
	private Color color;
	private String race;
	private Point mapLocation, townLocation;
	private ArrayList<Tile> tileList;
	private int money;
	private int food;
	private int energy;
	private int ore;
	private int crystite;
	private Image image1 = (new ImageIcon("src/player1.png")).getImage();
	private Image image2 = (new ImageIcon("src/player2.png")).getImage();
	private int order = 0;
	
	/**
	 * contains player information
	 * @param n name of player
	 * @param c color of player
	 * @param r race of player
	 * */
	public Player(String n, Color c, String r){
		name = n;
		color = c;
		race = r;
		playerIni();
		tileList = new ArrayList<Tile>();
		mapLocation = new Point(400, 300);
	}
	
	/**
	 * Players interface
	 * */
	private void playerIni(){
		if (race=="Flapper")
			money=1600;
		if (race=="Human")
			money=600;
		if (race=="Others")
			money=1000;
	}
	
	/**
	 * Drawing player inmage on map.
	 * */
	public void draw(Graphics g, int tw, int th){
		
		
		if(order==0){
			System.out.println("draw player");
			
		g.drawImage(image1, mapLocation.x, mapLocation.y, tw, th, null);
		order =1;
		}
		else if(order ==1){
			System.out.println("draw player");
			
		g.drawImage(image2, mapLocation.x, mapLocation.y, tw, th, null);
		order =0;
		}
	}
	
	/**
	 * @return player name
	 * */
	public String getName() {
		return name;
	}
	
	/**
	 * @return player color
	 * */
	public Color getColor() {
		return color;
	}
	
	/**
	 * @return player race
	 * */
	public String getRace() {
		return race;
	}
	
	/**
	 * Setting a location on map
	 * */
	public void setMapLocation(Point p) {
		mapLocation = p;
	}
	
	/**
	 * @return a location from map
	 * */
	public Point getMapLocation() {
		return mapLocation;
	}
	
	/**
	 * Setting town location to p
	 * */
	public void setTownLocation(Point p) {
		townLocation = p;
	}
	
	/**
	 * @return town location
	 * */
	public Point getTownLocation() {
		return townLocation;
	}
	
	/**
	 * @return player money
	 * */
	public int getMoney(){
		return money;
	}
	
	/**
	 * @return player food
	 * */
	public int getFood(){
		return food;
	}
	
	/**
	 * @return player energy
	 * */
	public int getEnergy(){
		return energy;
	}
	
	/***
	 * 
	 * @return player ore
	 */
	public int getOre(){
		return ore;
	}
	
	/***
	 * 
	 * @return player crystite
	 */
	public int getCrystite(){
		return crystite;
	}
	
	/**
	 * This method uses to buy tile 
	 * @param newTile a new tile have been bought
	 * @param state the current state of game
	 */
	public void buyTile(Tile newTile, NewGame.State state){
		int cost = newTile.latestPrice(state);
		if(money >= cost){
			money -= cost;
		    tileList.add(newTile);
		    newTile.beBought(this);
		    System.out.println(name + " buy tile that costs" + cost + ", and player has " + money + " left");
		}
		
	}
}
